package cn.tedu.egnotice1.controller;

import cn.tedu.egnotice1.base.response.JsonResult;
import cn.tedu.egnotice1.base.response.StatusCode;
import cn.tedu.egnotice1.mapper.NoticeMapper;
import cn.tedu.egnotice1.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1.pojo.dto.NoticeAddQuery;
import cn.tedu.egnotice1.pojo.dto.NoticeUpdateParam;
import cn.tedu.egnotice1.pojo.entity.Notice;
import cn.tedu.egnotice1.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egnotice1.pojo.vo.NoticeListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    NoticeMapper noticeMapper;

    @PostMapping("/add")
    public JsonResult add(@RequestBody NoticeAddParam noticeAddParam) {
        log.debug("noticeAddParam" + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(99L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int num = noticeMapper.addNotice(notice);
        if (num > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @GetMapping("list")
    public JsonResult list(NoticeAddQuery noticeAddQuery) {
        log.debug("noticeAddQuery=" + noticeAddQuery);
        List<NoticeListVO> noticeListVOs = noticeMapper.list(noticeAddQuery);
        return JsonResult.ok(noticeListVOs);
    }

    @GetMapping("detail")
    public JsonResult detail(Long id) {
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);
        return JsonResult.ok(noticeDetailInfoVO);
    }

    @PostMapping("del")
    public JsonResult del(Long id) {
        int num = noticeMapper.delete(id);
        if (num > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
    @PostMapping("update")
    public JsonResult update(@RequestBody NoticeUpdateParam noticeUpdateParam) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        int num = noticeMapper.update(notice);
        if (num > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
